package yxw.com.smartbutler.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;
import yxw.com.smartbutler.R;
import yxw.com.smartbutler.entity.MyUser;
import yxw.com.smartbutler.utils.L;

/*
 *  项目名： SmartButler
 *  包名:    yxw.com.smartbutler.ui
 *  文件名： RegistActivity
 *  创建者：小威.岳
 *  创建时间：2018/11/14 15:33
 *  描述： TODO
 */public class RegistActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText et_user;
    private EditText et_age;
    private EditText et_desc;
    private EditText et_pwd;
    private EditText et_repwd;
    private EditText et_email;
    private RadioGroup mRadioGroup;
    private Button btnRegisted;

    //性别
    private Boolean isGender = true;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regist);

        initView();
    }

    private void initView() {
        et_user = findViewById(R.id.et_user);
        et_age = findViewById(R.id.et_age);
        et_desc = findViewById(R.id.et_desc);
        et_pwd = findViewById(R.id.et_pwd);
        et_repwd = findViewById(R.id.et_repwd);
        et_email = findViewById(R.id.et_email);
        mRadioGroup = findViewById(R.id.mRadioGroup);
        btnRegisted = findViewById(R.id.btnRegisted);
        btnRegisted.setOnClickListener(this);

    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btnRegisted:
                //获取到输入框的值
                String name = et_user.getText().toString().trim();
                String age = et_age.getText().toString().trim();
                String desc = et_desc.getText().toString().trim();
                String pwd = et_pwd.getText().toString().trim();
                String repwd = et_repwd.getText().toString().trim();
                String email = et_email.getText().toString().trim();
                if(pwd.equals(repwd)) {

//判断是否为空
                    if (!TextUtils.isEmpty(name) & !TextUtils.isEmpty(age) & !TextUtils.isEmpty(desc) & !TextUtils.isEmpty(pwd) & !TextUtils.isEmpty(repwd) & !TextUtils.isEmpty(email)) {
                        //先把性别判断一下
                        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                            @Override
                            public void onCheckedChanged(RadioGroup group, int checkedId) {
                                if (checkedId == R.id.rb_boy) {
                                    isGender = true;
                                } else if (checkedId == R.id.rb_girl) {
                                    isGender = false;
                                }
                            }
                        });


                        if (TextUtils.isEmpty(desc)) {
                            desc = "这个人很懒，什么都没留下";

                        }
                        //注册
                        MyUser user = new MyUser();
                        user.setUsername(name);
                        user.setPassword(pwd);
                        user.setAge(Integer.valueOf(age));
                        user.setEmail(email);
                        user.setDesc(desc);
                        user.setSex(isGender);
                        user.signUp(new SaveListener<MyUser>() {

                            @Override
                            public void done(MyUser myUser, BmobException e) {
                                if(e==null){
                                    Toast.makeText(RegistActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                                    finish();
                                }else{
                                    L.i(e.toString());
                                    Toast.makeText(RegistActivity.this, "注册失败 :"+e.toString(), Toast.LENGTH_SHORT).show();
                                }
                            }
                        });





                    } else {
                        Toast.makeText(this, "输入框不能为空", Toast.LENGTH_SHORT).show();
                    }
                }else{
                    Toast.makeText(this, "两次输入的密码不一致", Toast.LENGTH_SHORT).show();

                }

                break;
        }
    }
}
